﻿@model WebShop.Models.CategoryShowModel

@{
    string min_price = Request.QueryString["min_price"];
    string max_price = Request.QueryString["max_price"];

    string generalUrl = Request.CurrentExecutionFilePath;
    System.Text.StringBuilder sBuilder = new System.Text.StringBuilder();
    sBuilder.Append(generalUrl);
    int order_delimiter = 0;
    foreach (string item in Request.QueryString.AllKeys)
    {
        if (item == "order")
        {
            order_delimiter = 1;  
            continue; 
        }

        string parameter = ((sBuilder.Length == generalUrl.Length) ? "?" : "&") + item + "=" + Request.QueryString[item];
        sBuilder.Append(parameter);
    }
    generalUrl = sBuilder.ToString();
    
    int default_order = (int)WebShop.DataService.ProductSort.NameAsc;
    int selected_order = default_order;
    
    if (Request.QueryString["order"].IsInt() && Model.Orders.ContainsKey(Int32.Parse(Request.QueryString["order"])))
    {
        selected_order = Int32.Parse(Request.QueryString["order"]);
    } 
}

<div class="row-fluid">
    <ul class="breadcrumb">
        <li>
            <i class="icon-chevron-left"></i> <a href="@Url.RouteUrl("Default", new RouteValueDictionary { { "controller", "index" }, { "action", "index" }})">@Model.Translate("main_page")</a>
        </li>
        @{
            int index1 = 1;
        }
        @foreach (var item in Model.Breadcrumbs)
        {
            <span class="divider">/</span>
            if (index1 != Model.Breadcrumbs.Count)
            {
                <li>
                    <a href="@Url.RouteUrl("Category", new RouteValueDictionary { { "id", item.Id } })">@item.Name</a>
                </li>
            }
            else
            {
                <li>
                    <li class="active">@item.Name</li>
                </li>
            }
            index1++;
        }
    </ul>
    <div class="row">
        <div class="span3">
            <form class="form-horizontal well category-filter" method="GET" action="@Url.Action("Category", "Index")">
                <h3>@Model.Translate("filters")</h3>

                <fieldset>
                    <h4>@Model.Translate("by_price")</h4>

                    <div class="control-group">
                        <label class="control-label" for="min_price">@Model.Translate("min_price")</label>
                        <div class="controls">
                            <input type="text" class="input-medium" id="min_price" name="min_price" value="@min_price">
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="max_price">@Model.Translate("max_price")</label>
                        <div class="controls">
                            <input type="text" class="input-medium" id="max_price" name="max_price" value="@max_price"/>
                        </div>
                    </div>
                </fieldset>
                
                <input type="hidden" name="order" value="@selected_order"/>

                <fieldset>
                    <div class="control-buttons">
                        <button class="btn btn-primary" type="submit">@Model.Translate("submit")</button>
                    </div>
                </fieldset>

            </form>
        </div>
        <div class="span9">
            <div class="sorting">
                &nbsp;
                <div class="sorting" >
                    <ul class="nav nav-pills">
                        <li class="dropdown">
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">@Model.Orders[selected_order]<b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                @{
                                    foreach (var item in Model.Orders)
                                    {
                                        string url = generalUrl + ((Request.QueryString.Count == order_delimiter) ? "?" : "&") + "order=" + item.Key;
                                        <li><a href="@url">@item.Value</a></li>
                                    }
                                }
                            </ul>
                        </li>
                    </ul>
                </div>
            </div>

            <div>
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>@Model.Translate("product_index")</th>
                            <th>@Model.Translate("product_name")</th>
                            <th>@Model.Translate("price")</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @{
                            int index = 1;
                        }
                        @foreach (var item in Model.Products)
                        {
                            <tr>
                                <td>@index</td>
                                <td class="product_name"><a href="@Url.RouteUrl("Product", new RouteValueDictionary { { "id", item.Id } })">@item.Name</a></td>
                                <td class="product_price">@String.Format("{0:00}", item.Price)</td>
                                <!--
                                <td class="product_count">@{
                            if (item.Count != 0)
                            {
                                                                  <button class="btn-success" type="button">To bill</button>
                            }
                 
                                                          }</td>
                                                          -->
                            </tr>
                            index++;
                        }
                    </tbody>
                </table>
            </div>

            <div class="pagination pagination-centered">
                <ul>
                    @for (int i = 0; i < Model.Pages; i++)
                    {
                        <li @if (i == Model.Page)
                            {<text> class="active" </text>}><a href="@Url.RouteUrl("Category", new RouteValueDictionary { { "id", Model.Category.Id }, { "page", i } })">@(i + 1)</a></li>
                    }
                </ul>
            </div>
        </div>
    </div>
</div>